//
//  LocalDataManager.m
//  rxtd_xfd
//
//  Created by clq on 15/4/15.
//  Copyright (c) 2015年 clq. All rights reserved.
//

#import "LocalDataManager.h"


//#import "ZipArchive.h"

@implementation LocalDataManager

#pragma mark - 单一实例
static LocalDataManager* sharedLocalDataManager = nil;

+(LocalDataManager*) sharedLocalDataManager
{
    @synchronized(self){  // 防止多线程同事访问
        if (sharedLocalDataManager == nil) {
            sharedLocalDataManager = [[LocalDataManager alloc] init];
        }
        return sharedLocalDataManager;
    }

}

-(void)setObjectWithNSUserDefaults:(id)val forUsername: (NSString *)key
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject: val forKey: key];
    [defaults synchronize];
}
-(id)getObjectWithNSUserDefaults:(NSString *)key
{
    return [[NSUserDefaults standardUserDefaults] objectForKey: key];
}
-(void)removeObjectWithNSUserDefaults:(NSString *)key
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    
    [defaults removeObjectForKey: key];
    [defaults synchronize];
}

/**
 *  存储图片到指定文件的路径
 *
 *  @param image              图片
 *  @param directoryName      目录名
 *  @param imageName          图片存储名称
 *  @param compressionQuality 压缩率
 *
 *  @return YES/NO
 */
- (BOOL)saveImage:(UIImage *)image directoryName:(NSString *)directoryName imageName:(NSString *)imageName compressionQuality:(CGFloat)compressionQuality {
    
    UIImage *bigImage = image;//[self imageWithImageSimple:image scaledToSize:CGSizeMake(400.0, 400.0)];
    
    NSString *documentPath = [self getDocumentsPath];
    if (![[NSFileManager defaultManager] fileExistsAtPath:[documentPath stringByAppendingPathComponent:directoryName]]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:[documentPath stringByAppendingPathComponent:directoryName] withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    NSString *imageSavePath = [NSString stringWithFormat:@"%@/%@/%@", documentPath, directoryName, imageName];
    NSData *imageData = UIImageJPEGRepresentation(bigImage, compressionQuality);
    BOOL isSuc = [imageData writeToFile:imageSavePath atomically:YES];
    return isSuc;
}

/**
 *  获取图片
 *
 *  @param fileName      图片名称
 *  @param directoryName 目录名
 *
 *  @return 图片
 */
- (UIImage *)getImageWithName:(NSString *)fileName directoryName:(NSString *)directoryName {
    
    NSString *path = [self getFilePathFromDocument:fileName directoryName:directoryName];
    
    return [self getImageFromPath:path];
}

- (UIImage *)getImageFromPath:(NSString *)path
{
    
    if (IsNilString(path)) {
        return nil;
    }else {
        return [UIImage imageWithContentsOfFile:path];
    }
}

/**
 *  Document里是否存在指定文件的路径
 *
 *  @param fileName      文件名
 *  @param directoryName 目录名
 *
 *  @return 文件的路径
 */
- (NSString *)getFilePathFromDocument:(NSString *)fileNname directoryName:(NSString *)directoryName {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [[documentsDirectory stringByAppendingPathComponent:directoryName] stringByAppendingPathComponent:fileNname];
}

/**
 *  获取document路径
 *
 *  @return document路径
 */
- (NSString *)getDocumentsPath {
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [pathArray objectAtIndex:0];
    return documentPath;
}

@end
